<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\service\AdminService;
use app\common\attributes\Menu;
use app\common\attributes\RandyValidate;
use app\common\validate\AdminValidate;
use think\annotation\Inject;
use think\response\Json;


class AdminController extends AdminBaseController
{
    #[Inject]
    protected AdminService $service;

    #[Inject]
    protected Admin $model;

    /**
     * 管理员列表
     *
     * @throws
     */
    #[Menu('管理员列表')]
    public function getAdminList()
    {
        $data = $this->getParams();
        //当前是否为关联查询
        //设置过滤方法
        $extendQueryHelper = $this->buildparams();
        $list = $this->service->getAdminList($extendQueryHelper, $data);
        foreach ($list as $row) {

        }
        return $this->success($list);

    }

    #[Menu('添加管理员')]
    #[RandyValidate(validate: AdminValidate::class, scene: 'add')]
    public function addAdminItem()
    {
        $data = $this->getParams();
        $res = AdminService::addAdminItem($data);
        return $this->success($res);
    }


    /**
     * 删除管理员
     *
     * @param $admin_id
     *
     */
    #[Menu('删除管理员')]
    #[RandyValidate(validate: AdminValidate::class, scene: 'del')]
    public function delAdminItem($admin_id)
    {
        $data = $this->getParams();
        $res = AdminService::deleteAdminItem($data['admin_id']);
        return $this->success($res);
    }

    /**
     * 管理员详情
     *
     * @param $admin_id
     *
     */
    #[Menu('查看管理员')]
    #[RandyValidate(validate: AdminValidate::class, scene: 'item')]
    public function getAdminItem()
    {
        $data = $this->getParams();
        $res = AdminService::getAdminItem($data['admin_id']);
        return $this->success($res);
    }

    /**
     * 修改管理员
     * @Validator(class=AdminValidate::class , scene="set")
     *
     */
    #[Menu('修改管理员')]
    #[RandyValidate(validate: AdminValidate::class, scene: 'set')]
    public function setAdminItem()
    {
        $data = $this->getParams();
        $res = AdminService::setAdminItem($data);
        return $this->success($res);
    }
    #[Menu('禁用管理员')]
    public function disable($admin_id) : Json
    {
        $res =  $this->service->disable($admin_id);

        return $this->returnSuccess($res);
    }
    #[Menu('解禁管理员')]
    public function enable($admin_id) : Json
    {
        return $this->success($this->service->enable($admin_id));

    }

    /**
     * 修改密码
     *
     * @author : Randy_chen
     * @Date   : 2022/3/27
     * @Time   : 10:09
     */
    #[Menu('修改管理员密码')]
    public function setAdminPassword($old_password, $password, $password_confirm) : Json
    {
        return $this->success($this->service->setAdminPassword($old_password, $password, $password_confirm));
    }
}
